package com.wq.struct;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: wangqiang20995
 * @Date:2019/2/26
 * @Description:
 * @Resource:
 */
public class LinkedStruct extends Struct {

    private Node head;

    private Node tail;

    private volatile int length;

    public LinkedStruct() {
        this.head = new Node(-1);
        this.tail = head;
        this.length = 0;
    }


    public void addNode2End(int node) {
        Node temp = new Node(node);
        if (this.head == this.tail) {
            this.head.next = temp;
            this.tail = temp;
        } else {
            this.tail.next = temp;
            this.tail = temp;
        }

        this.length++;
    }

    /**
     * @Author:wangqiang20995
     * @Datetime:2019/2/26 19:45
     * @param: [node, position]
     * @Description:添加node到指定位置的节点
     **/
    public void addNode(int position, int value) {
        if (position >= this.length) {
            throw new IllegalArgumentException("position[" + position + "]在链表中不存在");
        }

        Node node = this.head;
        for (int i = 0; i < position; i++) {
            node = node.next;
        }

        Node temp = new Node(value);
        temp.next = node.next;
        node.next = temp;
        this.length++;
    }

    public List<Node> queryNodeValue(int value) {
        List<Node> result = new ArrayList<>();

        Node trail = head;
        while (trail.next != null) {
            if (trail.number == value) {
                result.add(trail);
            }
            trail = trail.next;
        }

        return result;
    }

    public void deletePositionNode(int position) {
        if (position >= this.length) {
            throw new IllegalArgumentException("position[" + position + "]超出链表长度");
        }

        Node prev = head;
        Node trail = prev.next;

        for (int i = 0; i < position; i++) {
            prev = trail;
            trail = trail.next;

           if(trail == tail){
               tail = prev;
           }

        }

        prev.next = trail.next;
        trail.next = null;
    }

    public void deleteFirstFitValue(int value) {
        Node first = head;
        Node end = tail;
        Node trail = first.next;

        do {
            if (trail.number == value) {
                first.next = trail.next;
                trail.next = null;

                if (trail == end) {
                    tail = first;
                }
                break;
            } else {
                first = trail;
                trail = trail.next;
            }
        } while (first != end);

    }

    public Node getHead() {
        return this.head;
    }

    public Node getTail() {
        return this.tail;
    }

    public static void main(String args[]) {
        LinkedStruct linkedStruct = new LinkedStruct();
        linkedStruct.addNode2End(1);
        linkedStruct.addNode2End(2);
        linkedStruct.addNode2End(3);

        linkedStruct.addNode(1, 4);
        linkedStruct.addNode(2,6);
        linkedStruct.printStruct();

        linkedStruct.deletePositionNode(2);
        linkedStruct.printStruct();

        linkedStruct.deleteFirstFitValue(1);
        linkedStruct.printStruct();
    }

    @Override
    protected void printStruct() {
        Node node = this.head;
        do {
            if (node.number < 0) {
                continue;
            }
            System.out.printf("%d\t", node.number);
        } while ((node = node.next) != null);
        System.out.printf("\n");
        System.out.println("Head-->" + getHead());
        System.out.println("Tail-->" + getTail());
    }

    private class Node {
        private int number;

        public Node next;

        public Node(int number) {
            this.number = number;
        }


        @Override
        public String toString() {
            return "Node{" +
                    "number=" + number +
                    '}';
        }
    }
}
